package com.ruoyi.project.module.zxm;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.domain.YqgInformationProject;
import com.ruoyi.project.domain.YqgInformationRoom;
import com.ruoyi.project.module.zxm.tool.TimeLog;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.project.service.IYqgInformationProjectService;
import com.ruoyi.project.service.IYqgInformationRoomService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

@RestController
@RequestMapping("/zxm/room")
public class RoomController extends BaseController {

    @Resource
    private IYqgInformationRoomService roomService;

    @Resource
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;


    //首先是查房间类型
    //这是查询全天房间
    @TimeLog
    @RequestMapping("/normal")
    @Log(title = "查询房间",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult roomSearch(){
        //System.out.println("我进来了");
        List<YqgInformationRoom> result = roomService.selectByNoraml();

        return AjaxResult.success(result);

    }


    //根据房间类型查询
    @TimeLog
    @RequestMapping("/other/{val}")
    @Log(title = "查询房间",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult other(@PathVariable("val") String roomType){
        //System.out.println(roomType+"woshi roomtype");
        List<YqgInformationRoom> result = roomService.selectByType(roomType);
        return AjaxResult.success(result);

    }
    //查询正在打扫的
    @TimeLog
    @RequestMapping("/clean")
    @Log(title = "查询打扫房间",businessType = BusinessType.CLEAN,logicalKey = "handBrand")
    public AjaxResult clean(){
        //System.out.println(roomType+"woshi roomtype");
        List<YqgInformationRoom> result = roomService.selectByClean();
        return AjaxResult.success(result);
    }

    //查询需要叫醒的
    @TimeLog
    @RequestMapping("/wake")
    @Log(title = "查询叫醒",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult wake(){
        List<YqgInformationRoom> result = roomService.selectByWake();
        return AjaxResult.success(result);

    }

    //根据房间号查项目信息表
    @RequestMapping("/join")
    @Log(title = "根据房间号查项目信息表",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult join (@RequestBody String roomNumber){
        //根据房间号查询房间类型
        List<YqgInformationProject> result = roomService.selectJoin(roomNumber);
        return AjaxResult.success(result);
    }

    //留房离开，只修改房间状态
    @RequestMapping("/reservation")
    @Log(title = "留房离开",businessType = BusinessType.LEAVE,logicalKey = "handBrand")
    public AjaxResult reservation (@RequestBody String roomNumber){
        //根据房间号查询房间类型
        YqgInformationRoom result = roomService.selectByRoomNumber(roomNumber);
        result.setRoomStatus("2");
        return toAjax(roomService.updateYqgInformationRoomByRoom(result));
    }

    //点击房间加钟
    @TimeLog
    @RequestMapping("/bell")
    @Log(title = "点击房间加钟",businessType = BusinessType.ADDBELLS,logicalKey = "handBrand")
    public AjaxResult bell (@RequestBody YqgInformationRoom yqgInformationRoom) throws ParseException {
        //根据房间号查询房间类型
        YqgInformationRoom yqgInformationRoom1 =  roomService.bellRoom(yqgInformationRoom);

        return toAjax(1);
    }

    //换房
    @TimeLog
    @RequestMapping("/full")
    @Log(title = "换房",businessType = BusinessType.ALLDAY,logicalKey = "handBrand")
    public AjaxResult full (@RequestBody YqgInformationRoom yqgInformationRoom) throws ParseException {
        //根据房间号查询房间类型
        YqgInformationRoom yqgInformationRoom1 =  roomService.fullRoom(yqgInformationRoom);

        return toAjax(1);
    }

    //根据房间信息查询房间状态
    @TimeLog
    @RequestMapping("/status/{number}")
    @Log(title = "根据房间信息查询房间状态",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult CountNumber(@PathVariable String number) throws ParseException {
        List<YqgInformationRoom> result = roomService.selectByNumber(number);
        return AjaxResult.success(result);

    }
    //查询所有房间中空房的数量
    @RequestMapping("/count")
    @Log(title = "查询所有房间中空房的数量",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult roomCount(){
        int count =   roomService.selectNormalCount();
        return AjaxResult.success(count);
    }

    //查询空闲房间和使用中房间
    @RequestMapping("/status/count")
    @Log(title = "查询空闲房间和使用中房间",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult statusCount(){
        List<YqgInformationRoom> result =    roomService.selectStatusCount();
        return AjaxResult.success(result);
    }

    /**
     * 开房
     */
    @RequestMapping("/start")
    @Log(title = "开房",businessType = BusinessType.OPENING,logicalKey = "handBrand")
    public AjaxResult start(@RequestBody YqgInformationRoom yqgInformationRoom) throws ParseException {


      YqgInformationRoom yqgInformationRoom1 =  roomService.startRoom(yqgInformationRoom);


       return toAjax(1);
    }




    //查询休息到时的
    @RequestMapping("/latter")
    @Log(title = "查询休息到时的",businessType = BusinessType.OTHER,logicalKey = "handBrand")
    public AjaxResult latter()
    {
        List<YqgInformationRoom>  result = roomService.selectLatterInforamtion();
        return AjaxResult.success(result);
    }

    /**
     * 退房
     */
    //开房状态 0 是空房，1是使用中，2是已退房，3是留房，4是正在打扫
    @RequestMapping("/end")
    @Log(title = "退房",businessType = BusinessType.CHECKOUT,logicalKey = "handBrand")
    public AjaxResult end(@RequestBody YqgInformationRoom yqgInformationRoom) throws ParseException {
        //先将类型转换
        //System.out.println(yqgInformationRoom+"999");
        //先判断房间状态，是2的才能退房
        YqgInformationRoom result =   roomService.selectByRoomNumber(yqgInformationRoom.getRoomNumber());
        if(!result.getRoomStatus().equals("1")){
                throw new ServiceException("此房间目前不能退房");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date open = dateFormat.parse(yqgInformationRoom.getNote2());
        yqgInformationRoom.setCheckOutTime(open);
        //判断下是否有同来
        if (yqgInformationRoom.getWhetherLeave().equals("1")){
            yqgInformationRoom.setRoomStatus("3");
        }
        else {
            yqgInformationRoom.setRoomStatus("2");
        }
        yqgInformationRoom.setWakeUp("");
        yqgInformationRoom.setEntryName("");
        //根据房间号和房间状态查询插入的那条唯一记录
        insertConsumption(yqgInformationRoom);
        return toAjax(roomService.updateYqgInformationRoomByRoom(yqgInformationRoom));
    }



    //洁房
    @RequestMapping("/sanitary")
    @Log(title = "洁房",businessType = BusinessType.FINISH,logicalKey = "handBrand")
    public AjaxResult sanitary(@RequestBody YqgInformationRoom yqgInformationRoom){
        YqgInformationRoom yqgInformationRooms = roomService.selectByRoomNumber(yqgInformationRoom.getRoomNumber());

//        if (yqgInformationRooms.getRoomStatus().equals("1")){
//            throw new ServiceException("用户还未退房");
//        }
         if (yqgInformationRooms.getRoomStatus().equals("2")){
            yqgInformationRoom.setRoomStatus("4");


        }
        else  if (yqgInformationRooms.getRoomStatus().equals("4")){
            yqgInformationRoom.setRoomStatus("0");
        }


        return toAjax(roomService.updateYqgInformationRoomByRoom(yqgInformationRoom));

    }



    private void insertConsumption(YqgInformationRoom yqgInformationRoom) {
        YqgConsumptionDetails yqgConsumptionDetails = new YqgConsumptionDetails();
        //根据房间号和房间状态查询唯一的值
        yqgConsumptionDetails.setSeat(yqgInformationRoom.getRoomNumber());
        yqgConsumptionDetails.setCheckOutStatus("1");

        List<YqgConsumptionDetails> yqgConsumptionDetails1 = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);

        for (YqgConsumptionDetails consumptionDetails : yqgConsumptionDetails1) {
            //添加退房时间

            consumptionDetails.setCheckOutTime(yqgInformationRoom.getCheckOutTime());
            //添加退房状态
            consumptionDetails.setCheckOutStatus("2");
            //添加备注字段（存储房间号信息）
            consumptionDetails.setNote10("房间号"+yqgInformationRoom.getRoomNumber());
            yqgConsumptionDetailsService.updateYqgConsumptionDetails(consumptionDetails);
        }
    }
}
